//创建全局变量
var K= KDA = {};
//封装一个类
(function($) {
//初始化	
KDA.Cache = {
		default_zone: {}
	};	
//Cache继承某个对象，获取get、set、remove方法;
$.extend(KDA.Cache, {
	//取得zone对象中key属性的值，如果zone属性为空或者没有可以属性都返回null
	get: function(key, zone) {
		//给变量z赋值，zone不为空时取zone，如果参数zone为空则取default_zone
		var k = zone ? this[zone] : this.default_zone;
		//返回z的值，如果z对象的key属性为空，则返回null
		return k ? k[key] : null;
	},
	//给zone对象的key属性赋值，值等于value；如果zone为空，则新建一个只有key属性的对象，并给key属性
	set: function(key, value, zone) {
		//当zone为空的时候z等于null,否则等于zone
		var k = zone ? this[zone] : this.default_zone;
		if (!k)
			k = this[zone] = {};
		//设置z对象中名字为key的属性值为value
		k[key] = value;
	},
	remove: function(key, zone) {
		var k = zone ? this[zone] : this.default_zone;
		if (k) {
			delete k[key];
		}
	}
});
$.extend(KDA, {
	io: $.ajax,
	ajax: $.ajax,
	get: $.get,
	post: $.post,
	del: function(url, data, callback, type) {
		if ($.isFunction(data)) {
			type = type || callback;
			callback = data;
			data = undefined;
		}

		return $.ajax({
			type: 'delete',
			url: url,
			data: data,
			success: callback,
			dataType: type
		});
	},
	put: function(url, data, callback, type) {
		if ($.isFunction(data)) {
			type = type || callback;
			callback = data;
			data = undefined;
		}

		return $.ajax({
			type: 'put',
			url: url,
			data: data,
			success: callback,
			dataType: type
		});
	},
	/**
	 * option代表一个对象，应具有tpl,url,dataType等属性以及before,success等方法
	 *
	 */
	tpl: function(option) {
		var tplurl = option.tpl,
			before = option.before;
		if (tplurl.indexOf('/') != 0) {
			tplurl = "" + tplurl;
		}
		var opt = $.extend({}, option);
		var cb = function(tpl) {
			KDA.Cache.set(tplurl, tpl, 'template');
			if (!opt.url) {
				var dat = {};
				if ($.isFunction(before)) {
					dat = before(dat);
				}
				var result = juicer.to_html(tpl, dat);
				if ($.isFunction(option.success)) {
					option.success(result, dat, {});
				}
				return ;
			}
			opt['success'] = function(data) {
				var dat = $.extend({}, data);
				if ($.isFunction(before)) {
					dat = before(dat);
				}
				var result = juicer.to_html(tpl, dat);
				if ($.isFunction(option.success)) {
					option.success(result, dat, data);
				}
			};
			if (!opt.dataType) {
				opt.dataType = 'json';
			}
			K.io(opt);
		};
		var tpl_cache = KDA.Cache.get(tplurl, 'template');
		$.get(tplurl, null, cb, 'html');
	},
	log: function() {},
	define: function(name, opt, ignoreDefault) {
		var id = '#' + name.replace(/\./g, '_');
		this.log('define a module use name:', name);
		var module={};
		$.extend(module,{
			contentTarget: id
		});
		var obj = $.extend(module, opt);
		if (ignoreDefault === true) {
			K.Cache.set(name, obj, 'module-backup');
			return;
		}
	},
	//弹出提醒信息
	kmessager:function(ktitle,kmsg){
		$.messager.show({
			title:ktitle,
			msg:kmsg,
			timeout:2000,
			showType:'slide'
		});
	},
	 getwinwidth:function(width){
		 var getwidth=document.body.clientWidth-width;
		 return getwidth;
	 },
	 getwinheight:function(height){
		 var getheight=document.body.clientHeight-height;
		 return getheight;
	 },
	//模块
	cleanModule: function() {
		var bkms = K.Cache['module-backup'];
		$.each(bkms, function(k, v) {
			var opts = v.operators;
			$.each(opts || [], function(i, vv) {
				delete v[vv];
			});
			K.Cache.set(k, v, 'module');
		});
		delete K.Cache['module-backup'];
	},
	destroyDialog: function(dialog) {
		if (dialog) {
			$(dialog).dialog('close');
		} else {
			$('.ui-dialog *').remove();
		}
	},
	invoke: function(name) {
		if (!name) return;
		var args = $.makeArray(arguments),
			names = name.split('.'),
			methodName = names.pop(),
			moduleName = names.join('.');
		var module = KDA.Cache.get(moduleName, 'module-backup');
		if (!module) {
			this.log('invoke by ', name, ' but module ', moduleName, ' not exists.');
			return;
		}

		var method = module[methodName];
		if (!method) {
			this.log('invoke by ', name, ' but method ', methodName, ' not exists.');
			return;
		}

		if ($.isFunction(method)) {
			args.shift();
			args.unshift(module);
			return method.apply(module, args);
		}
		if (typeof(method) == 'object') {
			args.shift();
			var key = args.shift();
			return method[key];
		}
	}
  });
	window.initModule = function(name, label, data) {
		var id = '#' + name.replace(/\./g, '_');
		K.invoke('K.addTab', id, label);
		$('.ui-dialog *').remove();
		init(name, data);
	};
	
	window.ivk = function() {
		K.invoke.apply(K, $.makeArray(arguments));
	};
	
	window.init = function(name, data) {
		if (data) {
			K.invoke(name + '.init', data);
		} else {
			K.invoke(name + '.init');
		}
	};
})(jQuery);